﻿package com.tomvian.twodee.sideon.interactions 
{
	import flash.display.DisplayObject;
	import com.tomvian.twodee.common.actions.Action;
	import com.tomvian.twodee.common.interactions.Interaction;
	import com.tomvian.maths.Vector2D;
	
	/**
	 * Water
	 * @author Thomas Vian
	 */
	public class Water extends Interaction
	{
		//--------------------------------------------------------------------------
		//
		//  Properties
		//
		//--------------------------------------------------------------------------
		
		public var inWater:Action
		
		public var buoyancy:Number;
		public var resistance:Number;
		
		protected var _currentSurface:Number;
		
		//--------------------------------------------------------------------------
		//
		//  Getters / Setters
		//
		//--------------------------------------------------------------------------
		
		//--------------------------------------------------------------------------
		//
		//  Constructor
		//
		//--------------------------------------------------------------------------
		
		public function Water(water:Array, buoyancy:Number, resistance:Number, swimPoint:Vector2D = null) 
		{
			super(water, swimPoint);
			this.buoyancy = buoyancy;
			this.resistance = resistance;
		}
		
		//--------------------------------------------------------------------------
		//
		//  Methods
		//
		//--------------------------------------------------------------------------
		
		override public function interact():void 
		{
			if (enabled)
			{
				_testPoint.copy(actorMass.position);
				_testPoint.addMod(cameraOffset).addMod(actorMass.velocity).addMod(interactionPoint);
				
				wasInteracting = interacting;
				interacting = false;
				
				for (var i:uint = 0, l:uint = interactiveObjects.length; i < l; i++)
				{
					var water:DisplayObject = interactiveObjects[i];
					
					if (water.hitTestPoint(_testPoint.x, _testPoint.y, true))
					{
						interacting = true;
						
						if (!wasInteracting)
						{
							currentObject = water;
							_currentSurface = water.getBounds(water.stage).top;
							
							if(inWater) actor.currentAction = inWater;
						}
						
						actorMass.friction.multiplyMod(resistance);
						
						actorMass.velocity.y -= (1 - 4 / (4 + (_testPoint.y - _currentSurface))) * buoyancy;
						
						break;
					}
				}
			}
		}
	}
}